import { defineStore } from 'pinia'

/**
 * 运动状态管理
 * 存储用户运动计划、卡路里消耗等
 */
export const useExerciseStore = defineStore('exercise', {
  state: () => ({
    // 当日消耗卡路里
    caloriesBurned: 520,
    
    // 目标消耗卡路里
    caloriesTarget: 600,
    
    // 运动时间（分钟）
    exerciseTime: 45,
    
    // 目标运动时间（分钟）
    exerciseTimeTarget: 60,
    
    // 今日步数
    steps: 6540,
    
    // 目标步数
    stepsTarget: 10000,
    
    // 运动记录
    exerciseLog: [
      {
        id: 1,
        type: '快走',
        duration: 30, // 分钟
        caloriesBurned: 180,
        distance: 2.4, // 公里
        time: '08:30',
        completed: true
      },
      {
        id: 2,
        type: '哑铃训练',
        duration: 15, // 分钟
        caloriesBurned: 120,
        time: '18:00',
        completed: true
      },
      {
        id: 3,
        type: '瑜伽',
        duration: 20, // 分钟
        caloriesBurned: 220,
        time: '20:30',
        completed: true
      }
    ],
    
    // 推荐运动
    recommendedExercises: [
      {
        id: 101,
        name: '晨间慢跑',
        description: '在早晨进行一次轻松的慢跑，帮助启动新陈代谢',
        caloriesPerHour: 600,
        intensity: '中等',
        duration: 30, // 建议时长（分钟）
        image: 'https://images.unsplash.com/photo-1623200216581-32895ec37b5c?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
      },
      {
        id: 102,
        name: '循环训练',
        description: '结合有氧和力量训练的高效锻炼方式',
        caloriesPerHour: 850,
        intensity: '高',
        duration: 20, // 建议时长（分钟）
        image: 'https://images.unsplash.com/photo-1599058917212-d750089bc07e?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
      },
      {
        id: 103,
        name: '瑜伽',
        description: '改善柔韧性和核心力量的全身练习',
        caloriesPerHour: 400,
        intensity: '低',
        duration: 40, // 建议时长（分钟）
        image: 'https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80'
      }
    ],
    
    // 周运动计划
    weeklyPlan: {
      monday: [
        { name: '快走', duration: 30, time: '07:30' },
        { name: '力量训练', duration: 20, time: '19:00' }
      ],
      tuesday: [
        { name: '瑜伽', duration: 40, time: '07:00' }
      ],
      wednesday: [
        { name: '慢跑', duration: 30, time: '07:30' },
        { name: '拉伸', duration: 15, time: '20:00' }
      ],
      thursday: [
        { name: '循环训练', duration: 25, time: '19:00' }
      ],
      friday: [
        { name: '游泳', duration: 45, time: '18:00' }
      ],
      saturday: [
        { name: '户外骑行', duration: 60, time: '09:00' }
      ],
      sunday: [
        { name: '放松日', duration: 0, time: '全天' }
      ]
    }
  }),
  
  getters: {
    // 计算卡路里消耗进度百分比
    caloriesProgress: (state) => {
      return Math.round((state.caloriesBurned / state.caloriesTarget) * 100)
    },
    
    // 计算步数进度百分比
    stepsProgress: (state) => {
      return Math.round((state.steps / state.stepsTarget) * 100)
    },
    
    // 计算运动时间进度百分比
    exerciseTimeProgress: (state) => {
      return Math.round((state.exerciseTime / state.exerciseTimeTarget) * 100)
    },
    
    // 获取当天的运动计划
    todayExercises: (state) => {
      const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
      const today = new Date().getDay()
      return state.weeklyPlan[days[today]]
    },
    
    // 获取本周已完成运动数量
    weeklyCompletedExercises: (state) => {
      return state.exerciseLog.filter(exercise => exercise.completed).length
    }
  },
  
  actions: {
    // 添加运动记录
    addExercise(exercise) {
      this.exerciseLog.push({
        id: Date.now(),
        ...exercise,
        completed: true
      })
      
      // 更新消耗卡路里
      this.caloriesBurned += exercise.caloriesBurned
      
      // 更新运动时间
      this.exerciseTime += exercise.duration
      
      // 如果是有步数的运动，更新步数
      if (exercise.steps) {
        this.steps += exercise.steps
      }
    },
    
    // 删除运动记录
    removeExercise(exerciseId) {
      const index = this.exerciseLog.findIndex(exercise => exercise.id === exerciseId)
      if (index !== -1) {
        const exercise = this.exerciseLog[index]
        
        // 减去对应的卡路里和时间
        this.caloriesBurned -= exercise.caloriesBurned
        this.exerciseTime -= exercise.duration
        
        // 如果有步数，也减去对应步数
        if (exercise.steps) {
          this.steps -= exercise.steps
        }
        
        // 从数组中移除
        this.exerciseLog.splice(index, 1)
      }
    },
    
    // 更新步数
    updateSteps(newSteps) {
      this.steps = newSteps
    },
    
    // 更新周运动计划
    updateWeeklyPlan(day, plan) {
      this.weeklyPlan[day] = plan
    }
  }
}) 